<html>
<head>
<title>Listing 16.6. The Browser Sniffer</title>
</head>

<body>

<script language="JavaScript" type="text/javascript">
<!--


// One of these Boolean variables will be
// set to true based on the browser name
var its_ie = false
var its_ns = false
var its_opera = false
var its_webtv = false
var its_compatible = false

// One of these Boolean variables will be set to 
// true based on the Internet Explorer version
var its_ie2 = false
var its_ie3 = false
var its_ie4 = false
var its_ie5 = false
var its_ie55 = false
var its_ie6 = false
var its_ie4plus = false
var its_ie5plus = false
var its_ie55plus = false
var its_ie6plus = false

// One of these Boolean variables will be set to 
// true based on the Netscape version
var its_ns2 = false
var its_ns3 = false
var its_ns4 = false
var its_ns6 = false
var its_ns3plus = false
var its_ns4plus = false
var its_ns6plus = false

// One of these Boolean variables will be
// set to true based on the operating system
var its_win31 = false
var its_win95 = false
var its_win98 = false
var its_winme = false
var its_winnt = false
var its_win2000 = false
var its_winxp = false
var its_windows = false
var its_win32 = false
var its_mac68k = false
var its_macppc = false
var its_macos = false
var its_linux = false
var its_other_os = false

// This will be true of the browser supports some kind of DHTML
var dhtml_ok = false

// Let's work with lowercase letters to keep things simple
var user_agent = navigator.userAgent.toLowerCase()

// BROWSER NAME

// Use indexOf() to examine the userAgent string
// for telltale signs of the browser name

if (user_agent.indexOf("opera") != -1) { its_opera = true }
else if (user_agent.indexOf("webtv") != -1) { its_webtv = true }
else if (user_agent.indexOf("msie") != -1) { its_ie = true }
else if (user_agent.indexOf("mozilla") != -1) {

    // For "moziila", we need to rule out some other possibilities, first
    if ((user_agent.indexOf("compatible") == -1) && 
        (user_agent.indexOf("spoofer") == -1) && 
        (user_agent.indexOf("hotjava") == -1)) {
        its_ns = true
    }
    else { its_compatible = true }
}

// BROWSER VERSION

var major_version = parseInt(navigator.appVersion)
var full_version = parseFloat(navigator.appVersion)
var ie_start = user_agent.indexOf("msie")

if (ie_start != -1) {
    var version_string = user_agent.substring(ie_start + 5)
    major_version = parseInt(version_string)
    full_version = parseFloat(version_string)
}

// INTERNET EXPLORER
if (its_ie || its_webtv) {
    if (major_version < 3) { its_ie2 = true }
    else if (major_version == 3) { its_ie3 = true }
    else if (major_version == 4) { its_ie4 = true }
    else if (major_version == 5) { its_ie5 = true }
    else if (full_version == 5.5) { its_ie55 = true }
    else if (major_version == 6) { its_ie6 = true }

    if (major_version >= 4) { its_ie4plus = true }
    if (major_version >= 5) { its_ie5plus = true }
    if (full_version >= 5.5) { its_ie55plus = true }
    if (major_version >= 6) { its_ie6plus = true }
}

// NETSCAPE
if (its_ns) {
    if (major_version < 3) { its_ns2 = true }
    else if (major_version < 4) { its_ns3 = true }
    else if (major_version == 4) { its_ns4 = true }
    else if (major_version == 5) { its_ns6 = true }

    if (major_version >= 3) { its_ns3plus = true }
    if (major_version >= 4) { its_ns4plus = true }
    if (major_version >= 5) { its_ns6plus = true }
}

// OPERATING SYSTEM

// Use indexOf() to examine the userAgent string
// for telltale signs of the operating system

// WINDOWS 3.1
if ((user_agent.indexOf("windows 3.1") != -1) || 
    (user_agent.indexOf("win16") != -1) ||
    (user_agent.indexOf("16bit") != -1) ||
    (user_agent.indexOf("16-bit") != -1)) { its_win31 = true }

// WINDOWS 95
else if ((user_agent.indexOf("windows 95") != -1) || 
         (user_agent.indexOf("win95") != -1)) { its_win95 = true }

// WINDOWS ME
if (user_agent.indexOf("win 9x 4.90") != -1) { its_winme = true }

// WINDOWS 98
else if ((user_agent.indexOf("windows 98") != -1) || 
         (user_agent.indexOf("win98") != -1)) { its_win98 = true }

// WINDOWS XP
else if ((user_agent.indexOf("windows nt 5.1") != -1) || 
         (user_agent.indexOf("winnt 5.1") != -1)) { its_winxp = true }

// WINDOWS 2000
else if ((user_agent.indexOf("windows nt 5.0") != -1) || 
         (user_agent.indexOf("winnt 5.0") != -1)) { its_win2000 = true }

// WINDOWS NT
else if ((user_agent.indexOf("windows nt") != -1) || 
         (user_agent.indexOf("winnt") != -1)) { its_winnt = true }

// MAC 680x0
else if ((user_agent.indexOf("mac") != -1) && 
        ((user_agent.indexOf("68K") != -1) || 
         (user_agent.indexOf("68000") != -1))) { its_mac68k = true }

// MAC PowerPC
else if ((user_agent.indexOf("mac") != -1) && 
        ((user_agent.indexOf("ppc") != -1) || 
         (user_agent.indexOf("powerpc") != -1))) { its_macppc = true }

// LINUX
else if (user_agent.indexOf("linux") != -1) { its_linux = true }

// OTHER OS
else { its_other_os = true }

// PLATFORM

// Use the operating system booleans to
// determine the general platform

// MAC OS
if (its_mac68k || its_macppc) { its_macos = true}

// 32-BIT WINDOWS
if (its_win95 || its_win98 || its_winme || its_winnt || its_win2000 || its_winxp) {its_win32 = true}

// WINDOWS
if (its_win31 || its_win32) {its_windows = true}

// DHTML SUPPORT
if (document.getElementById || document.all || document.layers) {
    dhtml_ok = true
} 

document.write("<b>The complete userAgent string:</b><br>")
document.write(navigator.userAgent)
document.write("<p>")
document.write("<b>The browser name is:</b><br>")
if (its_ie) { document.write("Internet Explorer") }
else if (its_ns) { document.write("Netscape") }
else if (its_opera) { document.write("Opera") }
else if (its_webtv) { document.write("WebTV") }
else if (its_compatible) { document.write("Netscape compatible") }

document.write("<p>")
document.write("<b>The browser version is:</b><br>")
if (its_ie2) { document.write("2.x") }
else if (its_ie3) { document.write("3.x") }
else if (its_ie4) { document.write("4.x") }
else if (its_ie5) { document.write("5.x") }
else if (its_ie55) { document.write("5.5") }
else if (its_ie6plus) { document.write("6.x or later") }
else if (its_ns2) { document.write("2.x") }
else if (its_ns3) { document.write("3.x") }
else if (its_ns4) { document.write("4.x") }
else if (its_ns6plus) { document.write("6.x or later") }
else {document.write(full_version) }

document.write("<p>")
document.write("<b>The operating system is:</b><br>")
if (its_win31) { document.write("Windows 3.x") }
else if (its_win95) { document.write("Windows 95") }
else if (its_win98) { document.write("Windows 98") }
else if (its_winme) { document.write("Windows Me") }
else if (its_winnt) { document.write("Windows NT") }
else if (its_win2000) { document.write("Windows 2000") }
else if (its_winxp) { document.write("Windows XP") }
else if (its_mac68k) { document.write("Mac 680x0") }
else if (its_macppc) { document.write("Mac PowerPC") }
else if (its_linux) { document.write("Linux") }
else if (its_webtv) { document.write("WebTV") }
else if (its_other_os) { document.write("Other operating system") }

//-->
</script>

</body>
</html>

